package com.ruoyi.web.wuliu.service.impl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.web.wuliu.domain.MaterialCarVo;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.wuliu.mapper.MaterialMessageMapper;
import com.ruoyi.web.wuliu.domain.MaterialMessage;
import com.ruoyi.web.wuliu.service.IMaterialMessageService;

/**
 * 物流信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-15
 */
@Service
public class MaterialMessageServiceImpl implements IMaterialMessageService 
{
    @Autowired
    private MaterialMessageMapper materialMessageMapper;

    /**
     * 查询物流信息
     * 
     * @param id 物流信息主键
     * @return 物流信息
     */
    @Override
    public MaterialMessage selectMaterialMessageById(String id)
    {
        return materialMessageMapper.selectMaterialMessageById(id);
    }

    /**
     * 查询物流信息列表
     * 
     * @param materialMessage 物流信息
     * @return 物流信息
     */
    @Override
    public List<MaterialMessage> selectMaterialMessageList(MaterialMessage materialMessage)
    {
        return materialMessageMapper.selectMaterialMessageList(materialMessage);
    }

    /**
     * 新增物流信息
     * 
     * @param materialMessage 物流信息
     * @return 结果
     */
    @Override
    public int insertMaterialMessage(MaterialMessage materialMessage)
    {
        materialMessage.setCreateTime(DateUtils.getNowDate());
        materialMessage.setId(IdUtils.randomUUID());
        materialMessage.setStatus(1L);
        try{
            List<String> latLng = getLatLng(materialMessage.getStartPoint());
            materialMessage.setStartPointLat(latLng.get(0));
            materialMessage.setStartPointLng(latLng.get(1));
            List<String> latLng1 = getLatLng(materialMessage.getEndPoint());
            materialMessage.setEndPointLat(latLng1.get(0));
            materialMessage.setEndPointLng(latLng1.get(1));
        }catch (Exception e){

        }
        return materialMessageMapper.insertMaterialMessage(materialMessage);
    }

    /**
     * 修改物流信息
     * 
     * @param materialMessage 物流信息
     * @return 结果
     */
    @Override
    public int updateMaterialMessage(MaterialMessage materialMessage)
    {
        materialMessage.setUpdateTime(DateUtils.getNowDate());
        try{
            List<String> latLng = getLatLng(materialMessage.getStartPoint());
            materialMessage.setStartPointLat(latLng.get(0));
            materialMessage.setStartPointLng(latLng.get(1));
            List<String> latLng1 = getLatLng(materialMessage.getEndPoint());
            materialMessage.setEndPointLat(latLng1.get(0));
            materialMessage.setEndPointLng(latLng1.get(1));
        }catch (Exception e){

        }
        return materialMessageMapper.updateMaterialMessage(materialMessage);
    }

    /**
     * 批量删除物流信息
     * 
     * @param ids 需要删除的物流信息主键
     * @return 结果
     */
    @Override
    public int deleteMaterialMessageByIds(String[] ids)
    {
        return materialMessageMapper.deleteMaterialMessageByIds(ids);
    }

    /**
     * 删除物流信息信息
     * 
     * @param id 物流信息主键
     * @return 结果
     */
    @Override
    public int deleteMaterialMessageById(String id)
    {
        return materialMessageMapper.deleteMaterialMessageById(id);
    }

    public List<String> getLatLng(String addressName)throws Exception{
        List<String> list = new ArrayList<>();
        String url = "http://api.map.baidu.com/geocoder/v2/?address=" + addressName + "&output=json&ak=FMfZUCmdtPeg04YL9a0I0MYFG1VG2s1F";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // 3.解析API返回的数据
        JSONObject jsonObj = new JSONObject(response.toString());
        JSONObject location = jsonObj.getJSONObject("result").getJSONObject("location");
        double latitude = location.getDouble("lat");
        double longitude = location.getDouble("lng");
        list.add(String.valueOf(latitude));
        list.add(String.valueOf(longitude));
        return list;
    }

    @Override
    public List<MaterialCarVo> getUserOrder(String id) {
        return materialMessageMapper.getUserOrder(id);
    }
}
